home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Chat / frmChat.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  10.7 KB  |  249 lines

  1. VERSION 5.00
  2. Begin VB.Form frmChat 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "vbDirectPlay Chat"
  5.    ClientHeight    =   5085
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   7695
  9.    Icon            =   "frmChat.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5085
  14.    ScaleWidth      =   7695
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmdWhisper 
  17.       Caption         =   "Whisper"
  18.       Height          =   255
  19.       Left            =   5820
  20.       TabIndex        =   3
  21.       Top             =   4740
  22.       Width           =   1695
  23.    End
  24.    Begin VB.TextBox txtSend 
  25.       Height          =   285
  26.       Left            =   60
  27.       TabIndex        =   0
  28.       Top             =   4740
  29.       Width           =   5595
  30.    End
  31.    Begin VB.ListBox lstUsers 
  32.       Height          =   4545
  33.       Left            =   5760
  34.       TabIndex        =   2
  35.       Top             =   120
  36.       Width           =   1815
  37.    End
  38.    Begin VB.TextBox txtChat 
  39.       Height          =   4635
  40.       Left            =   60
  41.       Locked          =   -1  'True
  42.       MultiLine       =   -1  'True
  43.       ScrollBars      =   3  'Both
  44.       TabIndex        =   1
  45.       TabStop         =   0   'False
  46.       Top             =   60
  47.       Width           =   5595
  48.    End
  49. Attribute VB_Name = "frmChat"
  50. Attribute VB_GlobalNameSpace = False
  51. Attribute VB_Creatable = False
  52. Attribute VB_PredeclaredId = True
  53. Attribute VB_Exposed = False
  54. Option Explicit
  55. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  56. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  57. '  File:       frmChat.frm
  58. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  59. Implements DirectPlay8Event
  60. Private Sub cmdWhisper_Click()
  61.     Dim lMsg As Long, lOffset As Long
  62.     Dim sChatMsg As String
  63.     Dim oBuf() As Byte
  64.     If lstUsers.ListIndex < 0 Then
  65.         MsgBox "You must select a user in the list before you can whisper to that person.", vbOKOnly Or vbInformation, "Select someone"
  66.         Exit Sub
  67.     End If
  68.     If lstUsers.ItemData(lstUsers.ListIndex) = 0 Then
  69.         MsgBox "Why are you whispering to yourself?", vbOKOnly Or vbInformation, "Select someone else"
  70.         Exit Sub
  71.     End If
  72.     If txtSend.Text = vbNullString Then
  73.         MsgBox "What's the point of whispering if you have nothing to say..", vbOKOnly Or vbInformation, "Enter text"
  74.         Exit Sub
  75.     End If
  76.         
  77.     'Send this message to the person you are whispering to
  78.     lMsg = MsgWhisper
  79.     lOffset = NewBuffer(oBuf)
  80.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  81.     sChatMsg = txtSend.Text
  82.     AddStringToBuffer oBuf, sChatMsg, lOffset
  83.     txtSend.Text = vbNullString
  84.     dpp.SendTo lstUsers.ItemData(lstUsers.ListIndex), oBuf, 0, DPNSEND_NOLOOPBACK
  85.     UpdateChat "**<" & gsUserName & ">** " & sChatMsg
  86. End Sub
  87. Private Sub Form_Load()
  88.     'Oh good, we want to play a multiplayer game.
  89.     'First lets get the dplay connection started
  90.     'Here we will init our DPlay objects
  91.     InitDPlay
  92.     'Now we can create a new Connection Form (which will also be our message pump)
  93.     Set DPlayEventsForm = New DPlayConnect
  94.     'Start the connection form (it will either create or join a session)
  95.     If Not DPlayEventsForm.StartConnectWizard(dx, dpp, AppGuid, 20, Me) Then
  96.         Cleanup
  97.         End
  98.     Else 'We did choose to play a game
  99.         gsUserName = DPlayEventsForm.UserName
  100.         If DPlayEventsForm.IsHost Then
  101.             Me.Caption = Me.Caption & " (HOST)"
  102.         End If
  103.     End If
  104. End Sub
  105. Private Sub Form_Unload(Cancel As Integer)
  106.     Me.Hide
  107.     DPlayEventsForm.DoSleep 50
  108.     Cleanup
  109. End Sub
  110. Private Sub UpdateChat(ByVal sString As String)
  111.     'Update the chat window first
  112.     txtChat.Text = txtChat.Text & sString & vbCrLf
  113.     'Now limit the text in the window to be 16k
  114.     If Len(txtChat.Text) > 16384 Then
  115.         txtChat.Text = Right$(txtChat.Text, 16384)
  116.     End If
  117.     'Autoscroll the text
  118.     txtChat.SelStart = Len(txtChat.Text)
  119. End Sub
  120. Private Sub txtSend_KeyPress(KeyAscii As Integer)
  121.     Dim lMsg As Long, lOffset As Long
  122.     Dim sChatMsg As String
  123.     Dim oBuf() As Byte
  124.     If KeyAscii = vbKeyReturn Then
  125.         If txtSend.Text <> vbNullString Then 'Make sure they are trying to send something
  126.             'Send this message to everyone
  127.             lMsg = MsgChat
  128.             lOffset = NewBuffer(oBuf)
  129.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  130.             sChatMsg = txtSend.Text
  131.             AddStringToBuffer oBuf, sChatMsg, lOffset
  132.             txtSend.Text = vbNullString
  133.             KeyAscii = 0
  134.             dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  135.             UpdateChat "<" & gsUserName & ">" & sChatMsg
  136.         End If 'We won't set KeyAscii to 0 here, because if they are trying to
  137.                'send blank data, we don't care about the ding for hitting enter on
  138.                'an empty line
  139.     End If
  140. End Sub
  141. Private Function GetName(ByVal lID As Long) As String
  142.     Dim lCount As Long
  143.     GetName = vbNullString
  144.     For lCount = 0 To lstUsers.ListCount - 1
  145.         If lstUsers.ItemData(lCount) = lID Then 'This is the player
  146.             GetName = lstUsers.List(lCount)
  147.             Exit For
  148.         End If
  149.     Next
  150. End Function
  151. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  152.     'VB requires that we must implement *every* member of this interface
  153. End Sub
  154. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  155.     'VB requires that we must implement *every* member of this interface
  156. End Sub
  157. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  158.     'VB requires that we must implement *every* member of this interface
  159. End Sub
  160. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  161.     If dpnotify.hResultCode <> 0 Then
  162.         'For some reason we could not connect.  All available slots must be closed.
  163.         MsgBox "Connect Failed.  Error: 0x" & CStr(Hex$(dpnotify.hResultCode)) & "  - This sample will now close.", vbOKOnly Or vbCritical, "Closing"
  164.         DPlayEventsForm.CloseForm Me
  165.     End If
  166. End Sub
  167. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  168.     'VB requires that we must implement *every* member of this interface
  169. End Sub
  170. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  171.     Dim dpPeer As DPN_PLAYER_INFO
  172.     dpPeer = dpp.GetPeerInfo(lPlayerID)
  173.         
  174.     'Add this person to chat (even if it's me)
  175.     lstUsers.AddItem dpPeer.Name
  176.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) <> DPNPLAYER_LOCAL Then 'this isn't me, someone just joined
  177.         UpdateChat "- " & dpPeer.Name & " is chatting"
  178.         'If it's not me, include an ItemData
  179.         lstUsers.ItemData(lstUsers.ListCount - 1) = lPlayerID
  180.     End If
  181. End Sub
  182. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  183.     'VB requires that we must implement *every* member of this interface
  184. End Sub
  185. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  186.     Dim lCount As Long
  187.     'We only care when someone leaves.  When they join we will receive a 'MSGJoin'
  188.     'Remove this player from our list
  189.     For lCount = 0 To lstUsers.ListCount - 1
  190.         If lstUsers.ItemData(lCount) = lPlayerID Then 'This is the player
  191.             UpdateChat "-- " & lstUsers.List(lCount) & " is no longer chatting."
  192.             lstUsers.RemoveItem lCount
  193.             Exit For
  194.         End If
  195.     Next
  196. End Sub
  197. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  198.     'VB requires that we must implement *every* member of this interface
  199. End Sub
  200. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  201.     'VB requires that we must implement *every* member of this interface
  202. End Sub
  203. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  204.     Dim dpPeer As DPN_PLAYER_INFO
  205.     dpPeer = dpp.GetPeerInfo(lNewHostID)
  206.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) = DPNPLAYER_LOCAL Then 'I am the new host
  207.         Me.Caption = Me.Caption & " (HOST)"
  208.     End If
  209. End Sub
  210. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  211.     'VB requires that we must implement *every* member of this interface
  212. End Sub
  213. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  214.     'VB requires that we must implement *every* member of this interface
  215. End Sub
  216. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  217.     'VB requires that we must implement *every* member of this interface
  218. End Sub
  219. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  220.     'process what msgs we receive.
  221.     Dim lMsg As Long, lOffset As Long
  222.     Dim dpPeer As DPN_PLAYER_INFO, sName As String
  223.     Dim sChat As String
  224.     With dpnotify
  225.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  226.     Select Case lMsg
  227.     Case MsgChat
  228.         sName = GetName(.idSender)
  229.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  230.         UpdateChat "<" & sName & "> " & sChat
  231.     Case MsgWhisper
  232.         sName = GetName(.idSender)
  233.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  234.         UpdateChat "**<" & sName & ">** " & sChat
  235.     End Select
  236.     End With
  237. End Sub
  238. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  239.     'VB requires that we must implement *every* member of this interface
  240. End Sub
  241. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  242.     If dpnotify.hResultCode = DPNERR_HOSTTERMINATEDSESSION Then
  243.         MsgBox "The host has terminated this session.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  244.     Else
  245.         MsgBox "This session has been lost.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  246.     End If
  247.     DPlayEventsForm.CloseForm Me
  248. End Sub
  249.